package com.ysd.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysd.impl.ChanPinDaoImpl;
import com.ysd.impl.ProductDaoImpl;
import com.ysd.impl.ProductcategoryDaolmpl;
import com.ysd.entity.Product;
import com.ysd.entity.Productcategory;
public class ProductsServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//产品分类
		ProductcategoryDaolmpl pro = new ProductcategoryDaolmpl();
		List<Productcategory> produ = pro.getProductcategory();
		request.setAttribute("produ", produ);
		//根据产品分类id查对应的产品
		ProductDaoImpl pDao= new ProductDaoImpl();
		int cId = Integer.parseInt(request.getParameter("productCategory"));
		  response.setCharacterEncoding("UTF-8");
		List<Product> productList= pDao.getProductsList(cId);
		request.setAttribute("productList", productList);
		
		int pId = Integer.parseInt(request.getParameter("productCategory"));
		ProductcategoryDaolmpl pdcDao = new ProductcategoryDaolmpl();
		List<Productcategory> pc = pdcDao.getProductcategoryId(pId);
		request.setAttribute("pc", pc);
		
		String Pname= request.getParameter("productName");
		String cateId = request.getParameter("productCategory");
		ProductDaoImpl prDao = new ProductDaoImpl();
		
		if (cateId!=null) {
			int catId = Integer.parseInt(cateId);
			List<Product> pList = prDao.getProductsList(catId);
			request.setAttribute("pList", pList);
			request.setAttribute("catId", catId);	
			
		} else {
			List<Product> pList = prDao.getProductsList2(Pname);
			request.setAttribute("pList", pList);
		}
		
		
		
		
		
		request.getRequestDispatcher("productlist.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
